package com.aj.activity.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.aj.activity.R;
import com.aj.databases.entry.FamilyZoneInfo;
import com.aj.utils.Contants;
import com.aj.utils.PortraitLoaderUtil;

public class FamilyPhotoGridAdapter extends BaseAdapter {

	private List<FamilyZoneInfo> data;

	public static List<FamilyZoneInfo> chooseData = new ArrayList<FamilyZoneInfo>();

	private boolean isManager = false;

	private Context context;

	private String selfFlag;

	private HashMap<Integer, View> viewCache = new HashMap<Integer, View>();

	public FamilyPhotoGridAdapter(Context context, List<FamilyZoneInfo> data,
			String selfFlag) {
		this.context = context;
		this.data = data;
		this.selfFlag = selfFlag;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public class ViewHodler {
		ImageView image;
		TextView photo_name;
		CheckBox choose_photo_checkbox;

		public ImageView getImage() {
			return image;
		}

		public void setImage(ImageView image) {
			this.image = image;
		}

		public TextView getPhoto_name() {
			return photo_name;
		}

		public void setPhoto_name(TextView photo_name) {
			this.photo_name = photo_name;
		}

		public CheckBox getChoose_photo_checkbox() {
			return choose_photo_checkbox;
		}

		public void setChoose_photo_checkbox(CheckBox choose_photo_checkbox) {
			this.choose_photo_checkbox = choose_photo_checkbox;
		}

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = viewCache.get(position);
		ViewHodler viewHodler = null;
		final FamilyZoneInfo familyZoneInfo = data.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_familyphoto_gridview, null);
			viewHodler = new ViewHodler();
			viewHodler.image = (ImageView) convertView.findViewById(R.id.image);
			viewHodler.photo_name = (TextView) convertView
					.findViewById(R.id.photo_name);
			viewHodler.choose_photo_checkbox = (CheckBox) convertView
					.findViewById(R.id.choose_photo_checkbox);
			convertView.setTag(viewHodler);
			viewCache.put(position, convertView);
		} else {
			viewHodler = (ViewHodler) convertView.getTag();
		}
		String photonameString = familyZoneInfo.getZoneName();
		if (familyZoneInfo.getZoneId() != null) {
			viewHodler.photo_name.setText(photonameString + "("
					+ familyZoneInfo.getPhotoNumber() + ")");
			PortraitLoaderUtil.loadSrc(context, viewHodler.image, null,
					familyZoneInfo.getZoneUrl(), R.drawable.nomal_photo,
					R.drawable.nomal_photo);
		} else {
			viewHodler.photo_name.setText(photonameString);
		}
		if (selfFlag.equals(Contants.ALBUM_TYPE_SELF)) {
			if (position == (data.size() - 1)) {
				viewHodler.image.setImageResource(R.drawable.add_photo_button);
			} else {
				if (isManager) {
					viewHodler.choose_photo_checkbox
							.setVisibility(View.VISIBLE);
					viewHodler.choose_photo_checkbox.setChecked(false);
					viewHodler.choose_photo_checkbox
							.setOnCheckedChangeListener(new OnCheckedChangeListener() {

								@Override
								public void onCheckedChanged(
										CompoundButton buttonView,
										boolean isChecked) {
									if (isChecked) {
										chooseData.add(familyZoneInfo);
									} else {
										chooseData.remove(familyZoneInfo);
									}
								}
							});
				} else {
					viewHodler.choose_photo_checkbox.setVisibility(View.GONE);
				}
			}
		}

		return convertView;
	}

	public boolean isManager() {
		return isManager;
	}

	public void setManager(boolean isManager) {
		this.isManager = isManager;
		chooseData.clear();
		notifyDataSetChanged();
	}

}
